#include <iostream>
#include <string>

#include "Calendar_t.h"
#include "Appointment_t.h"

Appointment_t* createApp();
bool userSetDay(DayTime_t& time);
bool userSetStartHour(DayTime_t& time);

int main(int argc, char* argv[])
{
	bool res = false;
	Calendar_t calendar;

	Appointment_t* a1 = new Appointment_t();
	DayTime_t startTime1(Day_t::Sunday, 12, 0), endTime1(Day_t::Sunday, 16, 0);
	a1->setTime(startTime1, endTime1);
	a1->setSubject("a1");

	Appointment_t* a2 = new Appointment_t();
	DayTime_t startTime2(Day_t::Sunday, 10, 0), endTime2(Day_t::Sunday, 17, 0);
	a2->setTime(startTime2, endTime2);
	a2->setSubject("a2");

	Appointment_t* a3 = new Appointment_t();
	DayTime_t startTime3(Day_t::Tuesday, 10, 0), endTime3(Day_t::Tuesday, 11, 0);
	a3->setTime(startTime3, endTime3);
	a3->setSubject("a3");

	Appointment_t* a4 = new Appointment_t();
	DayTime_t startTime4(Day_t::Friday, 10, 0), endTime4(Day_t::Friday, 18, 0);
	a4->setTime(startTime4, endTime4);
	a4->setSubject("a4");

	Appointment_t* a5 = new Appointment_t();
	DayTime_t startTime5(Day_t::Tuesday, 20, 0), endTime5(Day_t::Tuesday, 21, 0);
	a5->setTime(startTime5, endTime5);
	a5->setSubject("a5");

	Appointment_t* a6 = new Appointment_t();
	DayTime_t startTime6(Day_t::Monday, 5, 0), endTime6(Day_t::Monday, 7, 0);
	a6->setTime(startTime6, endTime6);
	a6->setSubject("a6");

	res = calendar.insert(a1);
	res = calendar.insert(a2);
	res = calendar.insert(a3);
	res = calendar.insert(a4);
	res = calendar.remove(*a1);
	res = calendar.insert(a2);
	res = calendar.insert(a5);
	res = calendar.insert(a6);

	//calendar.print2(); // TODO: fix

	calendar.removeAll();

	delete a1; delete a2; delete a3; delete a4; delete a5; delete a6;


	////////
	if(1)
	{
		int cmd=-1;

		while(cmd!=0)
		{
			std::cout<<"Please choose:"<<std::endl;
			std::cout<<"	(1) Create appointment"<<std::endl;
			std::cout<<"	(2) Remove appointment"<<std::endl;
			std::cout<<"	(9) Print calendar"<<std::endl;
			std::cout<<"	(0) Exit"<<std::endl;
			std::cin>>cmd;

			switch(cmd)
			{
			case 1: //create
				{
					Appointment_t* a = createApp();
					if(calendar.insert(a))
						std::cout<<"Appointment inserted"<<std::endl;
					else
						std::cout<<"Failed to insert appointment"<<std::endl;
					break;
				}
			case 2: //remove
				{
					DayTime_t startTime;
					userSetDay(startTime);
					userSetStartHour(startTime);

					Appointment_t* a = calendar.find(startTime);
					if(a != NULL)
					{
						if(!calendar.remove(*a, true))
							std::cout<<"Failed to remove appointment"<<std::endl;
						else
							std::cout<<"Appointment removed"<<std::endl;
					}
					else
						std::cout<<"Appointment not found"<<std::endl;
					break;
				}
			case 9:
				system("cls");
				//calendar.print2(); // TODO: fix
				break;
			case 0:
				break;
			default:
				break;
			}
		}
	}

	getchar();
	return 0;
}

Appointment_t* createApp()
{
	Appointment_t* a = new Appointment_t;
	DayTime_t startTime, endTime;

	userSetDay(startTime);
	endTime.setDay(startTime.day());

	userSetStartHour(startTime);

	while(1)
	{
		size_t temp;
		std::cout<<"Enter appointment end hour (0-23):";
		std::cin>>temp;
		if(endTime.setHours(temp))
		{
			if(a->setTime(startTime, endTime))
				break;
			else
				std::cout<<"Invalid end time: should be larger than start time"<<std::endl;
		}
		else
			std::cout<<"Invalid end hour"<<std::endl;
	}

	while(1)
	{
		std::string subject;
		std::cout<<"Enter appointment subject:";
		std::cin>>subject;
		a->setSubject(subject);
		break;
	}

	return a;
}

bool userSetDay(DayTime_t& time)
{
	while(1)
	{
		int temp;
		std::cout<<"Enter appointment day (1:Sunday - 7:Saturday):";
		std::cin>>temp;
		if(temp>=1 && temp<=7)
		{
			if(time.setDay((Day_t)temp))
				break;
		}

		std::cout<<"Invalid day"<<std::endl;
	}

	return true;
}

bool userSetStartHour(DayTime_t& time)
{
	while(1)
	{
		size_t temp;
		std::cout<<"Enter appointment start hour (0-23):";
		std::cin>>temp;
		if(time.setHours(temp))
			break;
		else
			std::cout<<"Invalid start hour"<<std::endl;
	}

	return true;
}